#!/usr/bin/perl 
#===============================================================================#{{{
#
#         FILE:  vldownpatches.pl
#
#        USAGE:  ./vldownpatches.pl 
#
#  DESCRIPTION:  
#
#      OPTIONS:  ---
# REQUIREMENTS:  ---
#         BUGS:  ---
#        NOTES:  ---
#       AUTHOR:   (), <>
#      COMPANY:  
#      VERSION:  1.0
#      CREATED:  05/14/2008 10:34:54 PM BST
#     REVISION:  ---
#===============================================================================
#}}}
use File::Basename;
#use LWP::Simple;
use Net::FTP;
use strict;
use File::Temp qw/tempdir/;
use warnings;

sub getBinary {#{{{
	my $package = shift || die "PARAMETER NEEDED in getBinary\n";
	my $ftp = Net::FTP->new("slackware.osuosl.org", Passive =>1) or die "Cannot connect to some.host.name: $@";
	$ftp->login("anonymous");
	$ftp->binary;
	$ftp->cwd("/pub/slackware/slackware-12.0/patches/packages");
	my @list = $ftp->ls();

	my @packageFile = grep {/$package.*tgz$/} @list;
	die "Couldn't get $package.  Probably there's more than one with that name\n"
	if (scalar @packageFile > 1 );

	print "$packageFile[0]\n" ;
	my $fileName = $packageFile[0];
	chomp($fileName);
	return ($ftp, $fileName);
}	# ----------  end of subroutine getBinary  ----------#}}}




# main
my $package = shift || die "usage:", basename($0) , " package\n";

my ($ftp, $fileName) = getBinary($package);

my $dir = '/tmp/packages/';
mkdir $dir;
$fileName =~ m/([^-]+)/ ;
mkdir $dir.$1;
mkdir $dir.$1.'/src/';
die "erroooooor in get\n" unless $ftp->get($fileName, $dir.$1.'/'.$fileName);

$ftp->cwd("/pub/slackware/slackware-12.0/patches/source/$1");

foreach ($ftp->ls ) {
$ftp->get($_,$dir.$1.'/src/'.$_ ) ;
}
$ftp->quit;

chdir $dir.'/'.$1 ;
(my $tlz = $fileName) =~ s/tgz/tlz/g;
system("mkdir explode &&
   cp $fileName explode/ &&
   cd explode/ &&
   explodepkg $fileName &&
   rm $fileName &&
   requiredbuilder -v -y . &&
   makepkg -c n -l y ../$tlz");
unlink $fileName;
print "the package $tlz is in ${dir}$1\n";
exit;

#my $content = get("http://slackware.osuosl.org/slackware-12.0/patches/packages/");
#die "Couldn't get it!" unless  $content;
#my @content = split '\n', $content;
#my @hola =grep {/<a href=.$package.*tgz\"/} @content;
#print  @hola ,"\n";


# <a href="openssh-5.0p1-i486-1_slack12.0.tgz">
#http://slackware.osuosl.org/slackware-12.0/patches/source/
#http://slackware.osuosl.org/slackware-12.0/patches/packages/
# vim: set tabstop=4 shiftwidth=4 foldmethod=marker : ##
